package com.heifeng.cloudshelf.utils;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.downloader.Error;
import com.downloader.OnDownloadListener;
import com.downloader.OnProgressListener;
import com.downloader.PRDownloader;
import com.downloader.Progress;
import com.heifeng.cloudshelf.R;

import java.text.DecimalFormat;

public class ApkDownLoadUtils extends AsyncTask<Void, Progress, String> {

    private final String APKURL =
            "https://www.pgyer.com/apiv2/app/install?appKey=f49659aa29d245a82faa70e7ff44f91c" +
                    "&_api_key=1bf3c031d44b59cc72feec2f9a5bdc77";
    private Context _context;
    private OnDownLoadListener onDownLoadListener;
    private TextView tv_progress;
    private ProgressBar progress;

    public ApkDownLoadUtils(Context _context, OnDownLoadListener onDownLoadListener, TextView tv_progress, ProgressBar progress) {
        this._context = _context;
        this.onDownLoadListener = onDownLoadListener;
        this.progress = progress;
        this.tv_progress = tv_progress;
        initialize();
    }

    private void initialize() {
        PRDownloader.initialize(_context);
    }

    public void download() {
        PRDownloader.download(UpdateUtils.downLoadUrl, _context.getExternalCacheDir().getAbsolutePath(),
                _context.getResources().getString(R.string.app_name) + ".apk")
                .build().setOnProgressListener(progress -> {
            publishProgress(progress);
        }).start(new OnDownloadListener() {
            @Override
            public void onDownloadComplete() {
                Log.e("TAGS", "onDownloadComplete");
                if (null != onDownLoadListener) {
                    onDownLoadListener.onDownloadComplete(true);
                }
            }

            @Override
            public void onError(Error error) {
                Log.e("TAGS", "onError:" + error.toString());
                if (null != onDownLoadListener) {
                    onDownLoadListener.onDownloadComplete(false);
                }
            }
        });

    }

    @Override
    protected void onProgressUpdate(Progress... values) {
        super.onProgressUpdate(values);
        if (null != values[0]) {
            double s = (double) values[0].currentBytes / values[0].totalBytes * 100;
            String format;
            if (0 < s) {
                format = new DecimalFormat("0.00").format(s);
            } else {
                format = "0.00";
            }
            progress.setMax((int) values[0].totalBytes);
            progress.setProgress((int) values[0].currentBytes);
            tv_progress.setText(format+"%");
        }
    }

    @Override
    protected String doInBackground(Void... voids) {
        download();
        return null;
    }

    public interface OnDownLoadListener {
        void onDownloadComplete(boolean isError);

        void onDownloadProgress(long totalBytes, long currentBytes);
    }
}
